package dao.impl;

import dao.UserDao;
import dao.resultsetextractor.UserResultSetExtractor;
import dao.rowmapper.UserRowMapper;
import entity.User;
import framework.bean.factory.annotation.Component;
import framework.dao.template.JdbcTemplate;

import java.util.List;

@Component("userDao")
public class UserDaoImpl implements UserDao {

    private JdbcTemplate jdbcTemplate;

    public UserDaoImpl() {
        jdbcTemplate=new JdbcTemplate();
    }

    @Override
    public User selectByUsername(String username) {
        String sql=new StringBuffer()
                .append("select * ")
                .append("from t_user ")
                .append("where username=? ")
                .toString();
        return jdbcTemplate.query(sql,new UserResultSetExtractor(),username);
    }

    @Override
    public void insert(User user) {
        String sql=new StringBuffer()
                .append("insert into t_user ")
                .append("    (username,password,sex,edu,hobbies,description,regist_date,status) ")
                .append("values ")
                .append("    (?,?,?,?,?,?,?,?) ")
                .toString();
        jdbcTemplate.update(sql,user.getUsername(),
                                user.getPassword(),
                                user.getSex(),
                                user.getEdu(),
                                user.getHob(),
                                user.getDescription(),
                                user.getRegistDate(),
                                user.getStatus());
    }

    @Override
    public User selectByUsernameAndPassword(String username, String password) {
        String sql=new StringBuffer()
                .append("select * ")
                .append("from t_user ")
                .append("where username=? ")
                .append("and password=? ")
                .toString();
        List<User> users=jdbcTemplate.query(sql,new UserRowMapper(),username,password);
        return users.size()==0?null:users.get(0);
    }
}






















